package com.zegome.game.gomestory.main.scene.play.board;

import java.util.Random;

import aurelienribon.tweenengine.BaseTween;
import aurelienribon.tweenengine.TweenCallback;

import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.paypal.android.sdk.bo;
import com.zegome.game.action.IAction;
import com.zegome.game.action.IAction.IActionListener;
import com.zegome.game.entity.IEntity;
import com.zegome.game.entity.action.MoveAction;
import com.zegome.game.entity.action.ScaleAction;
import com.zegome.game.entity.action.SequenceEntityAction;
import com.zegome.game.entity.sprite.AnimatedSprite;
import com.zegome.game.gomestory.main.scene.BaseGameScreen;
import com.zegome.game.gomestory.main.scene.draw.Draw3Screen;
import com.zegome.game.gomestory.main.scene.play.PlayingScreen;
import com.zegome.game.main.IAnimator.IAnimatorListener;

public class AnimationEat {	
	
	public boolean isRunning;
	private Board board;
	private int r1,c1,r2,c2;
	private PikaEat pika1, pika2;
	private AnimatedSprite boom1;
	private AnimatedSprite boom2;
	private IAnimatorListener boom1Listener;
	private IAnimatorListener boom2Listener;
	float xCenter, yCenter;
	private boolean isAction;
	//public LineConnect lineConnect;
	
	
	public AnimationEat(Board board)
	{
		this.board = board;
	    //lineConnect = new LineConnect(0, 0);
	    //lineConnect.resetLine();
	}
	
	
	TextureRegion[] getRegionsBoom()
	{
		switch (new Random().nextInt(6)) {
		case 0:
			return board.screen.ctr.a.boom1;
		case 1:
			return board.screen.ctr.a.boom2;
		case 2:
			return board.screen.ctr.a.boom3;
		case 3:
			return board.screen.ctr.a.boom4;
		case 4:
			return board.screen.ctr.a.boom5;
		case 5:
			return board.screen.ctr.a.boom6;
		default:
			return null;
		}
	}
	
	public void start(int type, int row1, int col1, int row2, int col2)
	{	
		
		r1 = row1;
		c1 = col1;
		r2 = row2;
		c2 = col2;
		
		board.screen.removeChild(boom1);
		board.screen.removeChild(boom2);		
		board.screen.removeChild(pika1);
		board.screen.removeChild(pika2);
		
		pika1 = new PikaEat(board, type);
		pika2 = new PikaEat(board, type);
		
		xCenter = board.dataMap.xBegin+board.dataMap.wi1Pikachu*col1 + board.dataMap.xBegin+board.dataMap.wi1Pikachu*col2+board.dataMap.wi1Pikachu;
		xCenter = xCenter/2;
		yCenter = board.dataMap.yBegin+board.dataMap.hi1Pikachu*row1 + board.dataMap.yBegin+board.dataMap.hi1Pikachu*row2+board.dataMap.hi1Pikachu;
		yCenter = yCenter/2;
		
		run1(new Vector2(board.dataMap.xBegin+board.dataMap.wi1Pikachu*col1, board.dataMap.yBegin+board.dataMap.hi1Pikachu*row1), new Vector2(xCenter-board.dataMap.wi1Pikachu/2, yCenter-board.dataMap.hi1Pikachu*1.3f/2));
		run2(new Vector2(board.dataMap.xBegin+board.dataMap.wi1Pikachu*col2, board.dataMap.yBegin+board.dataMap.hi1Pikachu*row2), new Vector2(xCenter-board.dataMap.wi1Pikachu/2, yCenter-board.dataMap.hi1Pikachu*1.3f/2));
		
		board.screen.attachChild(pika1);
		board.screen.attachChild(pika2);
		isRunning = true;
	}
	
	public void init()
	{
		isRunning = false;
		boom1Listener = new IAnimatorListener() {
			@Override
			public void onAnimatorStartLoop(AnimatedSprite sprite, int remaining) {
			}

			@Override
			public void onAnimatorStart(AnimatedSprite sprite) {
			}

			@Override
			public void onAnimatorFinishLoop(AnimatedSprite sprite,
					int remaining) {
			}

			@Override
			public void onAnimatorFinish(AnimatedSprite sprite) {
				
				isRunning = false;
				
				boom1.setVisible(false);
				//lineConnect.setVisible(false);
				
				if(isAction && board.dataMap.stylePlay == 9 && board.dataMap.total == board.dataMap.totalPikaFix/2){
					board.diamondMod.genMap();
					for(int i=1; i<=board.dataMap.mapCol;i++)
						board.diamondMod.actionCol(i);				   
					return;
				}
				
				if(board.dataMap.stylePlay!=4){					
					board.moveProcessor.move(r1, c1, r2, c2);				
				}
				
				if(board.dataMap.total>0){
					if(board.searchEat.search()==null){
						board.effectArrangeMap.changeMap();
					}
				}
				
				if(isAction && board.dataMap.total==0){
					if(board.layerMode.data!=null && board.dataMap.stylePlay == 8){
						board.isPlaying = false;
						board.layerMode.open();
						return;
					}
					if(board.isInVS==false){
					if(board.isBot == false){
					board.isPlaying = false;
					board.screen.ctr.a.bgSong.pause();
					board.screen.ctr.a.mainSong.pause();
					int numberStar = 3 - (int)(((float)board.tickTime/(float)board.dataMap.time)*3*0.75f);
					if(board.isInDraw == false){
						int chapter = (PlayingScreen.GAME_LEVEL-1)/20+1;
						int level = PlayingScreen.GAME_LEVEL - (chapter-1)*20;
						if(board.screen.ctr.d.levels[chapter-1].starCollect[level-1] < numberStar){	
							board.screen.ctr.d.levels[chapter-1].starCollect[level-1] = numberStar;							
							
							if(board.screen.ctr.d.levels[chapter-1].maxLevel == level)
								board.screen.ctr.d.levels[chapter-1].maxLevel++;
							
							if(board.screen.ctr.d.levels[chapter-1].maxLevel>20){
								if(chapter>=board.screen.ctr.d.levels.length){							
								}
								else{
									int starAllColect=0;
									if(board.screen.ctr.d.levels != null){
										
										for(int i = 0; i< board.screen.ctr.d.chapters.length; i++){
											int starTotal = 0;											
											for(int j=0;j<board.screen.ctr.d.levels[i].starCollect.length;j++){									
												if(board.screen.ctr.d.levels[i].starCollect[j]!=-1)
													starTotal += board.screen.ctr.d.levels[i].starCollect[j];
											}
											if(board.screen.ctr.d.chapters[i].isUnLock)
											{	
												board.screen.ctr.d.chapters[i].starCollect = starTotal;
												starAllColect += starTotal;
											}
										}
									}
									if(board.screen.ctr.d.chapters[chapter].isUnLock == false){
								    if(starAllColect >= board.screen.ctr.d.chapters[chapter].starToUnLock){
									board.screen.ctr.d.chapters[chapter].isUnLock = true;
									board.screen.ctr.d.levels[chapter].maxLevel = 1;
								    }
								    }
								}
							}							
							board.screen.ctr.d.saveAllChapter();
						}
						else{
							
							//board.screen.ctr.d.levels[chapter-1].starCollect[level-1] = numberStar;							
							
							if(board.screen.ctr.d.levels[chapter-1].maxLevel == level)
								board.screen.ctr.d.levels[chapter-1].maxLevel++;
							
							if(board.screen.ctr.d.levels[chapter-1].maxLevel>20){
								if(chapter>=board.screen.ctr.d.levels.length){							
								}
								else{
									int starAllColect=0;
									if(board.screen.ctr.d.levels != null){
										
										for(int i = 0; i< board.screen.ctr.d.chapters.length; i++){
											int starTotal = 0;											
											for(int j=0;j<board.screen.ctr.d.levels[i].starCollect.length;j++){									
												if(board.screen.ctr.d.levels[i].starCollect[j]!=-1)
													starTotal += board.screen.ctr.d.levels[i].starCollect[j];
											}
											if(board.screen.ctr.d.chapters[i].isUnLock)
											{	
												board.screen.ctr.d.chapters[i].starCollect = starTotal;
												starAllColect += starTotal;
											}
										}
									}
									if(board.screen.ctr.d.chapters[chapter].isUnLock == false){
								    if(starAllColect >= board.screen.ctr.d.chapters[chapter].starToUnLock){
									board.screen.ctr.d.chapters[chapter].isUnLock = true;
									board.screen.ctr.d.levels[chapter].maxLevel = 1;
								    }
									}
								}
							}							
							board.screen.ctr.d.saveAllChapter();
						}
					}
					ChangeListener nextLis = new ChangeListener() {						
						@Override
						public void changed(ChangeEvent arg0, Actor arg1) {
							// TODO Auto-generated method stub
							
							if(board.isInDraw){
								
								Draw3Screen.INDEX_MAP++;
								if(Draw3Screen.INDEX_MAP>board.screen.ctr.d.maps.size()-1){
									Draw3Screen.INDEX_MAP = 0;
								}								
								
								int chapter = board.screen.ctr.d.maps.elementAt(Draw3Screen.INDEX_MAP).chapter;
								
								if(chapter==board.screen.ctr.a.currentChapter){						
								}
								else{													
									board.screen.ctr.a.loadRSGame(chapter, false);
									board.screen.ctr.controller.finishLoading();
									board.screen.ctr.a.loadedRSGame(chapter, false);			
								}
								board.start(board.screen.ctr.d.maps.elementAt(Draw3Screen.INDEX_MAP).chapter,board.screen.ctr.d.maps.elementAt(Draw3Screen.INDEX_MAP).map, board.screen.ctr.d.maps.elementAt(Draw3Screen.INDEX_MAP).mapRow, board.screen.ctr.d.maps.elementAt(Draw3Screen.INDEX_MAP).mapCol, board.screen.ctr.d.maps.elementAt(Draw3Screen.INDEX_MAP).time, board.screen.ctr.d.maps.elementAt(Draw3Screen.INDEX_MAP).numberPika, board.screen.ctr.d.maps.elementAt(Draw3Screen.INDEX_MAP).stylePlay);
							}
							else{
							
								final ChangeListener lis = new ChangeListener() {
									
									@Override
									public void changed(ChangeEvent arg0, Actor arg1) {
										// TODO Auto-generated method stub
										board.screen.fadeOut(new TweenCallback() {								
											@Override
											public void onEvent(int arg0, BaseTween<?> arg1) {
												// TODO Auto-generated method stub
												board.screen.ctr.a.bgSong.pause();
												board.screen.ctr.a.mainSong.pause();									
												board.screen.ctr.a.clearMusic2();
												board.screen.ctr.a.clearGame();									
												BaseGameScreen screen = board.screen;
												if(board.isInDraw){
													board.clear();
													//board = null;										
													board.screen.ctr.a.loadedDraw();										
													screen.getController().setScreen(screen.getController().getScreen("DrawScreen"));
												}
												else{
													board.clear();
													//board = null;
													board.screen.ctr.a.loadedChapter();
													screen.getController().setScreen(screen.getController().getScreen("ChapterScreen"));														
												}
											}
										});	
									}
								};
																
							if(PlayingScreen.GAME_LEVEL==20){
								board.screen.ctr.stageHelper.createDialog(board.screen.getStage(), board.screen.getTween(), board.screen.ctr.lang.getTextEndGame(20), 1,
									lis, null);
							}							
							else if(PlayingScreen.GAME_LEVEL==40){
								board.screen.ctr.stageHelper.createDialog(board.screen.getStage(), board.screen.getTween(), board.screen.ctr.lang.getTextEndGame(40), 1,
										lis, null);
							}
							else if(PlayingScreen.GAME_LEVEL==60){
								board.screen.ctr.stageHelper.createDialog(board.screen.getStage(), board.screen.getTween(), board.screen.ctr.lang.getTextEndGame(60), 1,
										lis, null);
							}
							else if(PlayingScreen.GAME_LEVEL==80){
								board.screen.ctr.stageHelper.createDialog(board.screen.getStage(), board.screen.getTween(), board.screen.ctr.lang.getTextEndGame(80), 1,
										lis, null);
							}
							else if(PlayingScreen.GAME_LEVEL==100){
								board.screen.ctr.stageHelper.createDialog(board.screen.getStage(), board.screen.getTween(), board.screen.ctr.lang.getTextEndGame(100), 1,
										lis, null);
							}
							else if(PlayingScreen.GAME_LEVEL==120){
								board.screen.ctr.stageHelper.createDialog(board.screen.getStage(), board.screen.getTween(), board.screen.ctr.lang.getTextEndGame(120), 1,
										lis, null);
							}
							else if(PlayingScreen.GAME_LEVEL==140){
								board.screen.ctr.stageHelper.createDialog(board.screen.getStage(), board.screen.getTween(), board.screen.ctr.lang.getTextEndGame(140), 1,
										lis, null);
							}
							else{
							PlayingScreen.GAME_LEVEL++;
							if(PlayingScreen.GAME_LEVEL > board.screen.ctr.d.chapters.length*20-1){
								PlayingScreen.GAME_LEVEL = 0;
							}
							
							if(board.screen.ctr.d.getChapter(PlayingScreen.GAME_LEVEL)==board.screen.ctr.a.currentChapter){						
							}
							else{													
								board.screen.ctr.a.loadRSGame(PlayingScreen.GAME_LEVEL, true);
								board.screen.ctr.controller.finishLoading();
								board.screen.ctr.a.loadedRSGame(PlayingScreen.GAME_LEVEL, true);
							}
							board.start(PlayingScreen.GAME_LEVEL);
							}
							}
						}
					};
					
					ChangeListener replayLis = new ChangeListener() {						
						@Override
						public void changed(ChangeEvent arg0, Actor arg1) {
							// TODO Auto-generated method stub
							if(board.isInDraw){
								board.start(board.screen.ctr.d.maps.elementAt(Draw3Screen.INDEX_MAP).chapter,board.screen.ctr.d.maps.elementAt(Draw3Screen.INDEX_MAP).map, board.screen.ctr.d.maps.elementAt(Draw3Screen.INDEX_MAP).mapRow, board.screen.ctr.d.maps.elementAt(Draw3Screen.INDEX_MAP).mapCol, board.screen.ctr.d.maps.elementAt(Draw3Screen.INDEX_MAP).time, board.screen.ctr.d.maps.elementAt(Draw3Screen.INDEX_MAP).numberPika, board.screen.ctr.d.maps.elementAt(Draw3Screen.INDEX_MAP).stylePlay);								
							}
							else{
							board.start(PlayingScreen.GAME_LEVEL);
							}
						}
					};
					
					ChangeListener menuLis = new ChangeListener() {						
						@Override
						public void changed(ChangeEvent arg0, Actor arg1) {
							// TODO Auto-generated method stub
							board.screen.fadeOut(new TweenCallback() {								
								@Override
								public void onEvent(int arg0, BaseTween<?> arg1) {
									// TODO Auto-generated method stub
									board.screen.ctr.a.bgSong.pause();
									board.screen.ctr.a.mainSong.pause();									
									board.screen.ctr.a.clearMusic2();
									board.screen.ctr.a.clearGame();									
									BaseGameScreen screen = board.screen;
									if(board.isInDraw){
										board.clear();
										//board = null;										
										board.screen.ctr.a.loadedDraw();										
										screen.getController().setScreen(screen.getController().getScreen("DrawScreen"));
									}
									else{
										board.clear();
										//board = null;
										board.screen.ctr.a.loadedLevel();
										screen.getController().setScreen(screen.getController().getScreen("LevelScreen"));														
									}
								}
							});
						}
					};
					
					if(board.screen.ctr.a.isHasDialog() == false)
						board.screen.ctr.stageHelper.createDialogWin(board.screen.getStage(), board.screen.getTween(), numberStar, board.pika, board.combo, board.dataMap.time - board.tickTime,board.isInDraw?0:board.screen.ctr.d.getChapter(board.level),board.isInDraw?0:board.level-1, menuLis, replayLis, nextLis);
					}
					}
				}
			}

			@Override
			public void onAnimatorChange(AnimatedSprite sprite, int newIndex,
					int oldIndex) {
			}
		};
		boom2Listener = new IAnimatorListener() {
			@Override
			public void onAnimatorStartLoop(AnimatedSprite sprite, int remaining) {
			}

			@Override
			public void onAnimatorStart(AnimatedSprite sprite) {
			}

			@Override
			public void onAnimatorFinishLoop(AnimatedSprite sprite,
					int remaining) {
			}

			@Override
			public void onAnimatorFinish(AnimatedSprite sprite) {
				boom2.setVisible(false);
			}

			@Override
			public void onAnimatorChange(AnimatedSprite sprite, int newIndex,
					int oldIndex) {
			}
		};
		//board.screen.removeChild(lineConnect);
		//board.screen.attachChild(lineConnect);
		//lineConnect.setVisible(false);
	}
	public void clear(){
		if(boom1!=null)
		board.screen.removeChild(boom1);
		if(boom2!=null)
		board.screen.removeChild(boom2);
		//board.screen.removeChild(lineConnect);
		boom1 = null;
		boom2 = null;
	}
	
	public void run1(Vector2 from, Vector2 to){		
		pika1.setSize(board.dataMap.wi1Pikachu, board.dataMap.hi1Pikachu);
		pika1.setOrigin(board.dataMap.wi1Pikachu/2, board.dataMap.hi1Pikachu/2);
		ScaleAction scale = new ScaleAction(0.3f, 1f, 1.3f);
		MoveAction move = new MoveAction(0.3f, from.x, to.x, from.y,to.y);		
		SequenceEntityAction sequence = new SequenceEntityAction(scale,move);		
		
		move.addActionListener(
		new IActionListener<IEntity>() {
			
			@Override
			public void onActionStarted(IAction<IEntity> arg0, IEntity arg1) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onActionFinished(IAction<IEntity> arg0, IEntity arg1) {
				// TODO Auto-generated method stub
				
			}
		});
		pika1.setPosition(from.x, from.y);
		pika1.registerAction(sequence);
	}
	
	public void run2(Vector2 from, Vector2 to){		
		isAction = false;
		
		if(board.dataMap.stylePlay == 9 && board.dataMap.total == board.dataMap.totalPikaFix/2){
			isAction = true;
		}		
				
		if(board.dataMap.total==0){
			isAction = true;
		}
		
		pika2.setSize(board.dataMap.wi1Pikachu, board.dataMap.hi1Pikachu);
		pika2.setOrigin(board.dataMap.wi1Pikachu/2, board.dataMap.hi1Pikachu/2);

		ScaleAction scale = new ScaleAction(0.3f, 1f, 1.3f);
		MoveAction move = new MoveAction(0.3f, from.x, to.x, from.y,to.y);		
		SequenceEntityAction sequence = new SequenceEntityAction(scale,move);				
		move.addActionListener(
		new IActionListener<IEntity>() {
			
			@Override
			public void onActionStarted(IAction<IEntity> arg0, IEntity arg1) {
				// TODO Auto-generated method stub				
			}
			
			@Override
			public void onActionFinished(IAction<IEntity> arg0, IEntity arg1) {
				// TODO Auto-generated method stub				
				pika1.setVisible(false);
				pika2.setVisible(false);
				
				TextureRegion[] boom = getRegionsBoom();
				
				boom1 = new AnimatedSprite(0.1f, 0, 0, 0, boom,boom1Listener);	
				boom2 = new AnimatedSprite(0.1f, 0, 0, 0, boom,boom2Listener);	
				
				boom1.setSize((boom[0].getRegionWidth()*board.dataMap.wi1Pikachu/58), (boom[0].getRegionHeight()*board.dataMap.hi1Pikachu/58));
				boom2.setSize((boom[0].getRegionWidth()*board.dataMap.wi1Pikachu/58), (boom[0].getRegionHeight()*board.dataMap.hi1Pikachu/58));
				
				boom1.setZIndex(0);		
				board.screen.attachChild(boom1);
				boom2.setZIndex(0);		
				board.screen.attachChild(boom2);
				int boom1X = (int) (xCenter-boom1.getWidth()/2);
				int boom1Y = (int) (yCenter-boom1.getHeight()/2);
				boom1.setPosition(boom1X, boom1Y);
				boom1.animate(0.1f);
				int boom2X = (int) (xCenter-boom2.getWidth()/2);
				int boom2Y = (int) (yCenter-boom2.getHeight()/2);
				boom2.setPosition(boom2X, boom2Y);
				boom2.animate(0.1f);
				//lineConnect.setVisible(true);
				
				if(board.isBot == false){
				int comboX = (int)(board.dataMap.xBegin+board.dataMap.wi1Pikachu*c1 + board.dataMap.xBegin+board.dataMap.wi1Pikachu*c2+board.dataMap.wi1Pikachu)/2;
				int comboY = (int)(board.dataMap.yBegin+board.dataMap.hi1Pikachu*r1 + board.dataMap.yBegin+board.dataMap.hi1Pikachu*r2+board.dataMap.hi1Pikachu)/2;
				
				Combo combo = new Combo(board,board.screen, board.selectProcessor.combo,50+50*board.selectProcessor.combo,comboX,comboY);
				board.screen.attachChild(combo);
				}
			}
		});
		pika2.setPosition(from.x, from.y);
		pika2.registerAction(sequence);
		
	}
}
